1400E - Clear the Multiset - CodeForces Solution


data structures divide and conquer dp greedy *2200

Please click on ads to support us..

C++ Code:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
 
//#define int long long 
#define endl '\n'
#define x first
#define y second
 
using namespace std;
 
const int N = 5010, M = 15, mod = 1e9+7, INF = 0x3f3f3f3f3f3f3f3f;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<long long , long long> PLL;

typedef pair<char,int> PCI;
typedef pair<int,pair<int,int>> PIII;
typedef pair<string,int> PSI;
typedef pair<double,double> PDD;
 
int n,m;
int a[N];
int f[N][N];//表示在第i个位置使用了j次操作1的最小答案
int mn[N];

void solve()
{ 
   cin>>n;
   for (int i=1;i<=n;i++)cin>>a[i];
   memset(f,0x3f,sizeof f);
   f[0][0]=0;
   for (int i=1;i<=n;i++)
   {
       mn[n+1]=INF;
       for (int j=n;j>=0;j--)mn[j]=min(mn[j+1],f[i-1][j]);
       int x = INF;
       for (int j=0;j<=min(a[i],n);j++)
       {
           x=min(x,f[i-1][j]-j);
           f[i][j]=min(mn[j],x+j)+(j<a[i]);
       }
   }
   int ans =  INF;
   for (int i=0;i<=min(a[i],n);i++)ans=min(ans,f[n][i]);
   cout<<ans<<endl;
}

signed main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    //init(N-1);
    int T=1;
    //cin>>T;
    while (T--) solve();
    return 0; 
}


Comments

Submit
0 Comments
More Questions

46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates